//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "NodeData.h"
using namespace Cephei::QL::Methods::Montecarlo;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Methods::Montecarlo::CNodeData::CNodeData (boost::shared_ptr<QuantLib::NodeData>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNodeData = NULL;
#endif
	_ppNodeData = &childNative;
    
}
Cephei::QL::Methods::Montecarlo::CNodeData::CNodeData (QuantLib::NodeData& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phNodeData = NULL;
#endif
	_ppNodeData = new boost::shared_ptr<QuantLib::NodeData> (&childNative);
    
    _NodeDataOwner = owner;
    
}

Cephei::QL::Methods::Montecarlo::CNodeData::CNodeData (CNodeData^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNodeData = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppNodeData = new boost::shared_ptr<QuantLib::NodeData> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Montecarlo::CNodeData::CNodeData (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNodeData = NULL;
#endif
	if (!t->IsSubclassOf(CNodeData::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Montecarlo::CNodeData::CNodeData (QuantLib::Handle<QuantLib::NodeData>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phNodeData = &childNative;
	_ppNodeData = &static_cast<boost::shared_ptr<QuantLib::NodeData>>(childNative.currentLink());
    
    _NodeDataOwner = owner;
}
Cephei::QL::Methods::Montecarlo::CNodeData::CNodeData (QuantLib::Handle<QuantLib::NodeData> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phNodeData = &childNative;
	_ppNodeData = &static_cast<boost::shared_ptr<QuantLib::NodeData>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Montecarlo::CNodeData::CNodeData (QuantLib::NodeData childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNodeData = NULL;
#endif
	_ppNodeData = new boost::shared_ptr<QuantLib::NodeData> (new QuantLib::NodeData (childNative));
    
}
#endif

Cephei::QL::Methods::Montecarlo::CNodeData::~CNodeData ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppNodeData != NULL)
    {
	    delete _ppNodeData;
        _ppNodeData = NULL;
    }
}
Cephei::QL::Methods::Montecarlo::CNodeData::!CNodeData ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppNodeData != NULL)
    {
	    delete _ppNodeData;
    }
}
QuantLib::NodeData& Cephei::QL::Methods::Montecarlo::CNodeData::GetReference ()
{
    if (_ppNodeData == NULL) throw REFNEW NativeNullException ();
	return **_ppNodeData;
}
boost::shared_ptr<QuantLib::NodeData>& Cephei::QL::Methods::Montecarlo::CNodeData::GetShared ()
{
    if (_ppNodeData == NULL) throw REFNEW NativeNullException ();
	return *_ppNodeData;
}
QuantLib::NodeData* Cephei::QL::Methods::Montecarlo::CNodeData::GetPointer ()
{
    if (_ppNodeData == NULL) throw REFNEW NativeNullException ();
	return &**_ppNodeData;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::NodeData>& Cephei::QL::Methods::Montecarlo::CNodeData::GetHandle ()
{
	if (_phNodeData == NULL)
	{
		_phNodeData = new Handle<QuantLib::NodeData> (*_ppNodeData);
	}
	return *_phNodeData;
}
#endif
bool Cephei::QL::Methods::Montecarlo::CNodeData::HasNative () 
{
	return (_ppNodeData != NULL);
}

Cephei::QL::Methods::Montecarlo::CNodeDataVector::CNodeDataVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Methods::Montecarlo::CNodeDataVector::~CNodeDataVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Methods::Montecarlo::CNodeDataVector::!CNodeDataVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Methods::Montecarlo::CNodeDataVector::CNodeDataVector (std::vector<QuantLib::NodeData>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Methods::Montecarlo::CNodeDataVector::CNodeDataVector (std::vector<QuantLib::NodeData> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::NodeData> (vec);
}
#endif
Cephei::QL::Methods::Montecarlo::CNodeDataVector::CNodeDataVector (std::vector<boost::shared_ptr<QuantLib::NodeData>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Methods::Montecarlo::CNodeDataVector::CNodeDataVector (std::vector<QuantLib::Handle<QuantLib::NodeData>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Methods::Montecarlo::CNodeDataVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Methods::Montecarlo::INodeData^ Cephei::QL::Methods::Montecarlo::CNodeDataVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw REFNEW RangeException ();
		QuantLib::NodeData& native = (*_pVValue)[index];
		return REFNEW CNodeData (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw REFNEW RangeException ();
		QuantLib::NodeData& native = *(*_pVBoost)[index];
		return REFNEW CNodeData (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw REFNEW RangeException ();
		QuantLib::NodeData& native = *((*_pVHand)[index].currentLink ());
		return REFNEW CNodeData (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Methods::Montecarlo::CNodeDataVector::default::set (int index, Cephei::QL::Methods::Montecarlo::INodeData^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CNodeData^>(value)->GetReference ());
		}
        else
        {
            QuantLib::NodeData& c = (*_pVValue)[index];
            c = dynamic_cast<CNodeData^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CNodeData^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::NodeData>& c = (*_pVBoost)[index];
		c = dynamic_cast<CNodeData^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::NodeData> ());
		}
		QuantLib::Handle<QuantLib::NodeData>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::NodeData>& b = static_cast<boost::shared_ptr<QuantLib::NodeData>>(c.currentLink());
		b = dynamic_cast<CNodeData^>(value)->GetShared ();
	}
#endif
}
Cephei::Core::Generic::ICoCell<Cephei::QL::Methods::Montecarlo::INodeData^>^ Cephei::QL::Methods::Montecarlo::CNodeDataVector::CItem::get (int index) 
{
	return CellFactory::Instance->Create<INodeData^> (dynamic_cast<INodeData^>(default[index]));
}

void Cephei::QL::Methods::Montecarlo::CNodeDataVector::CItem::set (int index, Cephei::Core::Generic::ICoCell<Cephei::QL::Methods::Montecarlo::INodeData^>^ value) 
{
	default[index] = safe_cast<INodeData^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::NodeData>& Cephei::QL::Methods::Montecarlo::CNodeDataVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::NodeData>>& Cephei::QL::Methods::Montecarlo::CNodeDataVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::NodeData>>& Cephei::QL::Methods::Montecarlo::CNodeDataVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Methods::Montecarlo::CNodeDataVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::NodeData> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::NodeData>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::NodeData>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

/* <summary>Create a single dimension array of NodeData that can be passed into other function </summary> */
IVector<Cephei::QL::Methods::Montecarlo::INodeData^>^ Cephei::QL::Methods::Montecarlo::CNodeData_Factory::CreateVector ()
 {
	 return REFNEW CoVector<INodeData^> (REFNEW CNodeDataVector ());
 }
IVector<Cephei::QL::Methods::Montecarlo::INodeData^>^ Cephei::QL::Methods::Montecarlo::CNodeData_Factory::CreateVector (PLATFORM::Collections::Generic::IEnumerable<INodeData^>^ source)
 {
	 return REFNEW CoVector<INodeData^> (source, REFNEW CNodeDataVector ());
 }
/* <summary>Create a single dimension array of NodeData with change events</summary> */
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Methods::Montecarlo::INodeData^>^>^>^ Cephei::QL::Methods::Montecarlo::CNodeData_Factory::CreateCellVector ()
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Methods::Montecarlo::INodeData^>^>^> (REFNEW CoCellVector<Cephei::QL::Methods::Montecarlo::INodeData^> (REFNEW CNodeDataVector ()));
 }
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Methods::Montecarlo::INodeData^>^>^>^ Cephei::QL::Methods::Montecarlo::CNodeData_Factory::CreateCellVector (PLATFORM::Collections::Generic::IEnumerable<Cephei::Core::Generic::ICoCell<Cephei::QL::Methods::Montecarlo::INodeData^>^>^ source)
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Methods::Montecarlo::INodeData^>^>^> (REFNEW CoCellVector<Cephei::QL::Methods::Montecarlo::INodeData^> (source, REFNEW CNodeDataVector ()));
 }

